package main

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"github.com/sirupsen/logrus"
	"hc_server/pkg/gredis"
	"hc_server/pkg/logging"
	"hc_server/pkg/rsa"
	"hc_server/pkg/setting"
	"hc_server/pkg/time"
	"hc_server/pkg/util"
	"hc_server/routers"
	"hc_server/routers/models"
	"net/http"
)

func init() {
	setting.Setup()
	models.Setup()
	logging.Setup()
	gredis.Setup()
	util.Setup()
	time.Setup()
	rsa.Setup()
}

func main() {
	gin.SetMode(setting.GetConfig().Server.RunMode)

	routersInit := routers.InitRouter()
	readTimeout := setting.GetConfig().Server.ReadTimeout
	writeTimeout := setting.GetConfig().Server.WriteTimeout
	endPoint := fmt.Sprintf(":%d", setting.GetConfig().Server.HttpPort)
	maxHeaderBytes := 1 << 20

	server := &http.Server{
		Addr:           endPoint,       //监听的 TCP 地址，格式为:8000
		Handler:        routersInit,    //http 句柄，实质为ServeHTTP，用于处理程序响应 HTTP 请求
		ReadTimeout:    readTimeout,    //允许读取的最大时间
		WriteTimeout:   writeTimeout,   //允许写入的最大时间
		MaxHeaderBytes: maxHeaderBytes, //请求头的最大字节数
	}

	logrus.Info("服务器开始运行")

	//开始监听服务
	err := server.ListenAndServe()
	if err != nil {
		logrus.Errorf("服务器启动失败: %s", err)
		return
	}
}
